<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">所属代理商ID：</label>
                        <div class="layui-input-inline mr0">
                            <input name="selAgtId" class="layui-input" type="text" placeholder="请输入所属代理商ID"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchLog" lay-submit><i class="layui-icon">&#xe615;</i>搜索</button>
                        <button id="agentDueBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                        <button id="agentDueBtnExport" class="layui-btn icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="agentDueTable" lay-filter="agentDueTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="agentDueTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="agentDueForm">
    <form lay-filter="agentDueForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">尽调信息编号</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入尽调信息编号" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属代理商ID</label>
            <div class="layui-input-block">
                <input name="selAgtId" placeholder="请输入所属代理商ID" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">尽调分类ID</label>
            <div class="layui-input-block">
                <input name="categoryId" placeholder="请输入尽调分类ID" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">尽调科目1ID</label>
            <div class="layui-input-block">
                <input name="subject1Id" placeholder="请输入尽调科目1ID" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">尽调科目1描述</label>
            <div class="layui-input-block">
                <textarea id="subject1Detail" name="subject1Detail" placeholder="请输入内容(200字以内)" class="layui-textarea" maxlength="200" ></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">尽调科目2ID</label>
            <div class="layui-input-block">
                <input name="subject2Id" placeholder="请输入尽调科目2ID" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">尽调科目2描述</label>
            <div class="layui-input-block">
                <textarea id="subject2Detail" name="subject2Detail" placeholder="请输入内容(200字以内)" class="layui-textarea" maxlength="200" ></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">尽调科目3ID</label>
            <div class="layui-input-block">
                <input name="subject3Id" placeholder="请输入尽调科目3ID" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">尽调科目3描述</label>
            <div class="layui-input-block">
                <textarea id="subject3Detail" name="subject3Detail" placeholder="请输入内容(200字以内)" class="layui-textarea" maxlength="200" ></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">评估的附件</label>
            <div class="layui-input-block">
                <input name="enclosure" placeholder="请输入评估的附件" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">评估结果</label>
            <div class="layui-input-block">
                <select name="result">
                        <option value="pass">符合且可以通过</option>
                        <option value="check">符合在考察考察</option>
                        <option value="waitPass">不符合但可以通过</option>
                        <option value="inconformity">不符合</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">审核人</label>
            <div class="layui-input-block">
                <input name="checkById" placeholder="请输入审核人" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">整体评估描述</label>
            <div class="layui-input-block">
                <textarea id="detail" name="detail" placeholder="请输入内容(200字以内)" class="layui-textarea" maxlength="200" ></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <select name="status">
                        <option value="oncheck">待审核</option>
                        <option value="check">已审核</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="agentDueFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin',
    'layedit'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var layedit = layui.layedit;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#agentDueTable',
            url: config.base + 'enterprise/sell/admin/agentDue/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'}
                ,{field: 'code', sort: true, title: '尽调信息编号'}
                ,{field: 'selAgtId', sort: true, title: '所属代理商ID'}
                ,{field: 'categoryId', sort: true, title: '尽调分类ID'}
                ,{field: 'subject1Id', sort: true, title: '尽调科目1ID'}
                ,{field: 'subject2Id', sort: true, title: '尽调科目2ID'}
                ,{field: 'subject3Id', sort: true, title: '尽调科目3ID'}
                ,{field: 'enclosure', sort: true, title: '评估的附件'}
                ,{field: 'resultDesc', sort: true, title: '评估结果'}
                ,{field: 'checkById', sort: true, title: '审核人'}
                ,{field: 'checkTime', sort: true, title: '审核时间'}
                ,{field: 'statusDesc', sort: true, title: '状态'}
                ,{field: 'createTime', sort: true, title: '创建时间'}
                ,{align: 'center', toolbar: '#agentDueTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if(menuButton.indexOf("搜索") == -1){
                    $('.layui-form.toolbar').remove();
                }
                if(menuButton.indexOf("修改") == -1){
                    $("[lay-event='edit']").remove();
                }
                if(menuButton.indexOf("删除") == -1){
                    $("[lay-event='del']").remove();
                }
            }
        });

        // 工具条点击事件
        table.on('tool(agentDueTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(agentDueTable)', function(obj){
            table.reload('agentDueTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 添加按钮点击事件
        $('#agentDueBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '1080px',
                offset: '65px',
                title: data ? '修改代理经销商尽调记录表' : '添加代理经销商尽调记录表',
                content: $('#agentDueForm').html(),
                success: function () {
                    var layeditIndex14 = layedit.build('subject1Detail',{
                        uploadImage: {
                            url: config.upload_url + 'layuiUpload?access_token=' +config.getToken(),
                            type: 'post'
                        }
                    });

                    var layeditIndex16 = layedit.build('subject2Detail',{
                        uploadImage: {
                            url: config.upload_url + 'layuiUpload?access_token=' +config.getToken(),
                            type: 'post'
                        }
                    });

                    var layeditIndex18 = layedit.build('subject3Detail',{
                        uploadImage: {
                            url: config.upload_url + 'layuiUpload?access_token=' +config.getToken(),
                            type: 'post'
                        }
                    });

                    var layeditIndex23 = layedit.build('detail',{
                        uploadImage: {
                            url: config.upload_url + 'layuiUpload?access_token=' +config.getToken(),
                            type: 'post'
                        }
                    });

                    if (data){
                        var option = $("form select[name='result']").find("option");
                        layui.each(option, function(index, item){
                            if(data.result  == item.text){
                                data.result = item.value;
                            }
                        });
                    }

                    if (data){
                        var option = $("form select[name='status']").find("option");
                        layui.each(option, function(index, item){
                            if(data.status  == item.text){
                                data.status = item.value;
                            }
                        });
                    }

                    form.val('agentDueForm', data);
                    // 表单提交事件
                    form.on('submit(agentDueFormSubmit)', function (d) {
                        layer.load(2);
                        d.field.subject1Detail = layedit.getContent(layeditIndex14);
                        d.field.subject2Detail = layedit.getContent(layeditIndex16);
                        d.field.subject3Detail = layedit.getContent(layeditIndex18);
                        d.field.detail = layedit.getContent(layeditIndex23);
                        admin.req(data ? 'enterprise/sell/admin/agentDue/update' : 'enterprise/sell/admin/agentDue/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('agentDueTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/sell/admin/agentDue/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#agentDueBtnExport').click(function () {
            var checkRows = table.checkStatus('agentDueTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>